# -*-coding:utf-8-*-

__author__ = 'nickleo'

import jsonpickle as json
from config import HogWartsConfig as cfg
import http_client as http
import manager_constant


def add_topic(cluster, topic, namesvr):
    statusCode = 500
    alert = 'error'
    http_topiclist_result = http.add_topic(namesvr, cluster, topic)
    if http_topiclist_result is None or http_topiclist_result.strip() == '':
        alert = ' add error nameserver:' + namesvr + ' topic:' + topic + "cluster:" + cluster
    else:
        statusCode = 200
        alert = http_topiclist_result

    return statusCode, alert


def del_topic(cluster, topic, namesvr):
    statusCode = 500
    alert = 'err'
    http_topiclist_result = http.del_topic(namesvr, cluster, topic)
    if http_topiclist_result is None or http_topiclist_result.strip() == '':
        alert = ' add error nameserver:' + namesvr + ' topic:' + topic + "cluster:" + cluster
    else:
        if http_topiclist_result == 'sucess':
            statusCode = 200
            alert = 'ok'
    return statusCode, alert


def add_subgroup(cluster, subgroup, namesvr):
    statusCode = 200
    alert = 'ok'
    http_topiclist_result = http.add_sub_group(namesvr, cluster, subgroup)
    if http_topiclist_result is None or http_topiclist_result.strip() == '':
        alert = ' add_subgroup nameserver:' + namesvr + ' subgroup:' + subgroup + "cluster:" + cluster
    else:
        statusCode = 200
        alert = http_topiclist_result
    return statusCode, alert


def del_subgroup(cluster, subgroup, namesvr):
    statusCode = 500
    alert = 'err'
    http_topiclist_result = http.del_subgroup(namesvr, cluster, subgroup)
    if http_topiclist_result is None or http_topiclist_result.strip() == '':
        alert = ' del_subgroup  nameserver:' + namesvr + ' subgroup:' + subgroup + "cluster:" + cluster
    else:
        if http_topiclist_result == 'success':
            statusCode = 200
            alert = 'ok'
    return statusCode, alert


def get_config():
    return manager_constant.brokerCfgText, manager_constant.mqNettyCfgText


def up_broker_cfg(type, namesvr, typeValue, key, value):
    statusCode = 200
    alert = 'ok'

    if type == 'cluster':
        http_topiclist_result = http.up_broker_cfg(namesvr, typeValue, key, value)
        if http_topiclist_result is None or http_topiclist_result.strip() == '':
            alert = ' up_broker_cfg  nameserver:' + namesvr + ' cluster :' + typeValue + "key:" + key + "value:" + value
        else:
            if http_topiclist_result == 'success':
                statusCode = 200
                alert = 'ok'
    elif type == 'addr':
        http_topiclist_result = http.up_broker_cfg_addr(namesvr, typeValue, key, value)
        if http_topiclist_result is None or http_topiclist_result.strip() == '':
            alert = ' up_broker_cfg_addr  nameserver:' + namesvr + ' addr :' + typeValue + "key:" + key + "value:" + value
        else:
            if http_topiclist_result == 'success':
                statusCode = 200
                alert = 'ok'
    else:
        statusCode = 400
        alert = 'what type? '
    alert = 'use cluster please'
    return statusCode, alert


def get_clusters_and_topics():
    clusters = {}
    topics = {}
    action_alert = None
    for name_server in cfg().get_nameservers():
        print 'name_server' + name_server
        http_topiclist_result = http.get_topiclist(name_server)
        if http_topiclist_result is None or http_topiclist_result.strip() == '':
            action_alert = 'nameserver:' + name_server + 'is No TopicRoute info No  ConsumerGroup '
        else:
            for _topiclist in json.decode(http_topiclist_result):
                # _topiclist ={"ClusterName":"FetionMQCluster","Topic":"FetionPerformace","ConsumerGroup":"FetionPerformance"}
                if _topiclist['ClusterName'] is None or _topiclist['ClusterName'].strip() == '':
                    x = 1
                else:
                    clusters[_topiclist['ClusterName']] = name_server
                if _topiclist['Topic'] is None or _topiclist['Topic'].strip() == '':
                    x = 1
                else:
                    topics[_topiclist['Topic']] = name_server

    return clusters, topics, action_alert


def get_broker_addrs():
    brokerAddrs = {};
    action_alert = None
    for name_server in cfg().get_nameservers():
        http_topiclist_result = http.get_topiclist(name_server)

        if http_topiclist_result is None or http_topiclist_result.strip() == '':
            action_alert = 'nameserver:' + name_server + 'is No TopicRoute info '
        else:
            for _topiclist in json.decode(http_topiclist_result):
                # _topiclist ={"ClusterName":"FetionMQCluster","Topic":"FetionPerformace","ConsumerGroup":"FetionPerformance"}
                http_topicroute_result = http.get_topicroute(name_server, _topiclist['Topic'])
                # {
                # "brokerDatas":[
                # {
                # "brokerAddrs":{0:"192.168.143.130:10911",1:"192.168.143.133:10911"
                # },
                # "brokerName":"fetionmq"
                # 		}
                # 	],
                # 	"filterServerTable":{},
                # 	"queueDatas":[
                # 		{
                # 			"brokerName":"fetionmq",
                # 			"perm":7,
                # 			"readQueueNums":1,
                # 			"topicSynFlag":0,
                # 			"writeQueueNums":1
                # 		}
                # 	]
                # }
                if http_topicroute_result is not None and http_topicroute_result.strip() != '':
                    http_topicroute = eval(http_topicroute_result)
                    brokerDatas = http_topicroute['brokerDatas']
                    if brokerDatas is not None:
                        for brokerDataTmp in brokerDatas:
                            for (k, v) in brokerDataTmp['brokerAddrs'].items():
                                brokerAddrs[v] = name_server

    return brokerAddrs, action_alert


def get_broker_clusters():
    result = {}
    action_alert = None
    for name_server in cfg().get_nameservers():
        http_result = http.get_clusterlist(name_server)
        if http_result is None or http_result.strip() == '':
            action_alert = 'nameserver:' + name_server + 'is No Clusters '
        else:

            for cluster in json.decode(http_result):
                result[cluster['ClusterName']] = name_server

    return result, action_alert